<?php
/**
 * 设置支付密码
 * @author yanghj 2014.5.17
 */
class Pay_passwordApp extends MemberbaseApp
{
    var $_user_id;
    var $_user_safety_mod;
    var $_member_from;

    function __construct()
    {
        $this->Pay_passwordApp();
    }

    function Pay_passwordApp()
    {
        parent::FrontendApp();
        $this->_user_id = intval($this->visitor->get('user_id'));
        $this->_member_from = isset($_GET['from']) ? trim($_GET['from']) : 'buyer';
        $this->_user_safety_mod = &m('usersafety');
    }

    function index()
    {
        header("Location:index.php");
    }  

    /**
     * 设置支付密码
     *
     */
    function set_pay_password()
    {
        $set_type = trim($_GET['set_type']);       
        
        //提交表单设置密码
        if($_SESSION['set_pay_password_step']==2)
        {
            if (!IS_POST)
            {
                $this->show_message('no_valid_data','goback', 'index.php?app=account_security&act=authen_verify&mode=set_pay_password');                       
            }
            /* 两次密码输入必须相同 */
            $new_password       = trim($_POST['new_password']);
            $confirm_password   = trim($_POST['confirm_password']);
            $password_level     = (int)($_POST['password_level'])<1 ? 1 :(int)($_POST['password_level']); 
            if ($new_password != $confirm_password)
            {
                $this->show_warning('两次输入的密码不相符');
                return;
            }
            if (!$new_password)
            {
                $this->show_warning('没有输入新密码');
                return;
            }
            $passlen = strlen($new_password);
            if ($passlen < 6 || $passlen > 20)
            {
                $this->show_warning('密码长度错误，应在6到20位之间');

                return;
            }
            
            $data = array(
                'pay_password' => md5(md5($new_password,true)),
                'pay_password_level' => $password_level,
                'user_id' => $this->_user_id,
                );
            $this->_user_safety_mod->add_edit($data, $this->_user_id);
            if ($this->_user_safety_mod->has_error()) {
                $this->show_warning($this->_user_safety_mod->get_error());   
                return;
            }
            //跳转到手机设置方式页面           
            if($set_type == 'mobile')
            {
               header("Location:index.php?app=account_security&act=authen_verify&mode=set_pay_password&set_type=mobile&step=3&from=" . $this->_member_from);
               return; 
            }
            //跳转到邮箱设置方式页面           
            if($set_type == 'email')
            {
               header("Location:index.php?app=account_security&act=authen_verify&mode=set_pay_password&set_type=email&step=3&from=" . $this->_member_from);
               return; 
            }           
        }       
    }
    
    /**
     * 修改支付密码
     *
     */
    function edit_pay_password()
    {
        if (!IS_POST)
        {
            $this->show_message('no_valid_data','goback', 'index.php?app=account_security&act=authen_verify&mode=edit_pay_password');                       
        }
       /* 两次密码输入必须相同 */
        $old_password       = trim($_POST['old_password']);
        $new_password       = trim($_POST['new_password']);
        $confirm_password   = trim($_POST['confirm_password']);
        $password_level     = (int)($_POST['password_level'])<1 ? 1 :(int)($_POST['password_level']); 
        if ($new_password != $confirm_password)
        {
            $this->show_warning('两次输入的密码不相符');

            return;
        }
        if (!$new_password)
        {
            $this->show_warning('没有输入新密码');
            return;
        }
        $passlen = strlen($new_password);
        if ($passlen < 6 || $passlen > 20)
        {
            $this->show_warning('密码长度错误，应在6到20位之间');
            return;
        }
        $pay_password = $this->_user_safety_mod->getOne("SELECT pay_password FROM {$this->_user_safety_mod->table} WHERE user_id = {$this->_user_id} ");
        if($pay_password != md5(md5($old_password,true)))
        {
            $this->show_warning('旧支付密码不正确');
            return;
            
        }
        
        $data = array(
            'pay_password' => md5(md5($new_password,true)),
            'pay_password_level' => $password_level,
            'user_id' => $this->_user_id,
            );
        $this->_user_safety_mod->add_edit($data, $this->_user_id);
        if ($this->_user_safety_mod->has_error()) {
            $this->show_warning($this->_user_safety_mod->get_error());   
            return;
        }
        $this->show_message('修改支付密码成功','返回安全设置','index.php?app=account_security&from=' . $this->_member_from);       
    }
    
    /**
     * 设置支付密码
     *
     */
    function find_pay_password()
    {
        $set_type = trim($_GET['set_type']);       
        
        //提交表单找回密码
        if($_SESSION['find_pay_password_step']==2)
        {
            if (!IS_POST)
            {
                $this->show_message('no_valid_data','goback', 'index.php?app=account_security&act=authen_verify&mode=set_pay_password');                       
            }
            /* 两次密码输入必须相同 */
            $new_password       = trim($_POST['new_password']);
            $confirm_password   = trim($_POST['confirm_password']);
            $password_level     = (int)($_POST['password_level'])<1 ? 1 :(int)($_POST['password_level']); 
            if ($new_password != $confirm_password)
            {
                $this->show_warning('两次输入的密码不相符');
                return;
            }
            if (!$new_password)
            {
                $this->show_warning('没有输入新密码');
                return;
            }
            $passlen = strlen($new_password);
            if ($passlen < 6 || $passlen > 20)
            {
                $this->show_warning('密码长度错误，应在6到20位之间');

                return;
            }
            
            $data = array(
                'pay_password' => md5(md5($new_password,true)),
                'pay_password_level' => $password_level,
                'user_id' => $this->_user_id,
                );
            $this->_user_safety_mod->add_edit($data, $this->_user_id);
            if ($this->_user_safety_mod->has_error()) {
                $this->show_warning($this->_user_safety_mod->get_error());   
                return;
            }
            //跳转到手机找回方式页面           
            if($set_type == 'mobile')
            {
               header("Location:index.php?app=account_security&act=authen_verify&mode=find_pay_password&set_type=mobile&step=3&from=" . $this->_member_from);
               return; 
            }
            //跳转到邮箱找回方式页面           
            if($set_type == 'email')
            {
               header("Location:index.php?app=account_security&act=authen_verify&mode=find_pay_password&set_type=email&step=3&from=" . $this->_member_from);
               return; 
            }           
        }       
    }
    
    /**
     * ajax检查支付密码是否正确
     *
     */
    function ajaxCheckPayPassword()
    {
        $old_password = trim($_POST['pay_password']);
        $pay_password = $this->_user_safety_mod->getOne("SELECT pay_password FROM {$this->_user_safety_mod->table} WHERE user_id = {$this->_user_id} ");
        if($pay_password != md5(md5($old_password,true)))
        {
            echo ecm_json_encode(false);
            return;
            
        }       
        echo ecm_json_encode(true);
    }   
    
}
?>
